package com.iknow.xmpp.smack.caps;

import java.io.PrintStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.collections.ReferenceMap;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverInfo.Feature;
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;

public class CapsManager
{
  private Connection mConnection;
  private Map<String, DiscoverInfo> mJidCache;
  private String mNode;
  private ServiceDiscoveryManager mSdm;
  private List<String> mSupportedAlgorithm;
  private Map<String, DiscoverInfo> mVerCache = new ReferenceMap();

  public CapsManager(ServiceDiscoveryManager paramServiceDiscoveryManager, Connection paramConnection)
  {
    this.mJidCache = new ReferenceMap();
    this.mSupportedAlgorithm = new ArrayList();
    this.mSdm = paramServiceDiscoveryManager;
    this.mConnection = paramConnection;
    init();
  }

  private String calculateVer(DiscoverInfo paramDiscoverInfo, String paramString)
    throws NoSuchAlgorithmException
  {
    Iterator localIterator2;
    StringBuilder localStringBuilder = new StringBuilder();
    Iterator localIterator1 = getSortedIdentity(paramDiscoverInfo).iterator();
    if (!(localIterator1.hasNext()))
      localIterator2 = getSortedFeature(paramDiscoverInfo).iterator();
    while (true)
    {
      while (!(localIterator2.hasNext()))
      {
        return StringUtils.encodeBase64(getHash(paramString, localStringBuilder.toString().getBytes()));
        DiscoverInfo.Identity localIdentity = (DiscoverInfo.Identity)localIterator1.next();
        String str1 = localIdentity.getCategory();
        if (str1 != null)
          localStringBuilder.append(str1);
        localStringBuilder.append('/');
        String str2 = localIdentity.getType();
        if (str2 != null)
          localStringBuilder.append(str2);
        localStringBuilder.append('/');
        localStringBuilder.append('/');
        String str3 = localIdentity.getName();
        if (str3 != null)
          localStringBuilder.append(str3);
        localStringBuilder.append('<');
      }
      localStringBuilder.append((String)localIterator2.next());
      localStringBuilder.append('<');
    }
  }

  private byte[] getHash(String paramString, byte[] paramArrayOfByte)
    throws NoSuchAlgorithmException
  {
    return MessageDigest.getInstance(paramString).digest(paramArrayOfByte);
  }

  private DiscoverInfo getOwnInformation()
  {
    DiscoverInfo localDiscoverInfo = new DiscoverInfo();
    DiscoverInfo.Identity localIdentity = new DiscoverInfo.Identity("client", ServiceDiscoveryManager.getIdentityName());
    localIdentity.setType(ServiceDiscoveryManager.getIdentityType());
    localDiscoverInfo.addIdentity(localIdentity);
    Iterator localIterator = this.mSdm.getFeatures();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return localDiscoverInfo;
      localDiscoverInfo.addFeature((String)localIterator.next());
    }
  }

  private List<String> getSortedFeature(DiscoverInfo paramDiscoverInfo)
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = paramDiscoverInfo.getFeatures();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        Collections.sort(localArrayList);
        return localArrayList;
      }
      localArrayList.add(((DiscoverInfo.Feature)localIterator.next()).getVar());
    }
  }

  private List<DiscoverInfo.Identity> getSortedIdentity(DiscoverInfo paramDiscoverInfo)
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = paramDiscoverInfo.getIdentities();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        Collections.sort(localArrayList, new Comparator(this)
        {
          public int compare(, DiscoverInfo.Identity paramIdentity2)
          {
            int k;
            String str1 = paramIdentity1.getCategory();
            if (str1 == null)
              str1 = "";
            String str2 = paramIdentity2.getCategory();
            if (str2 == null)
              str2 = "";
            int i = str1.compareTo(str2);
            if (i != 0)
              k = i;
            while (true)
            {
              while (true)
              {
                return k;
                String str3 = paramIdentity1.getType();
                if (str3 == null)
                  str3 = "";
                String str4 = paramIdentity2.getCategory();
                if (str4 == null)
                  str4 = "";
                int j = str3.compareTo(str4);
                if (j == 0)
                  break;
                k = j;
              }
              k = 0;
            }
          }
        });
        return localArrayList;
      }
      localArrayList.add((DiscoverInfo.Identity)localIterator.next());
    }
  }

  private void init()
  {
    initSupportedAlgorithm();
    PacketExtensionFilter localPacketExtensionFilter = new PacketExtensionFilter("c", "http://jabber.org/protocol/caps");
    this.mConnection.addPacketListener(new PacketListener(this)
    {
      public void processPacket()
      {
        if (paramPacket.getFrom().equals(CapsManager.access$0(this.this$0).getUser()));
        while (true)
        {
          CapsExtension localCapsExtension;
          do
          {
            return;
            localCapsExtension = (CapsExtension)paramPacket.getExtension("c", "http://jabber.org/protocol/caps");
          }
          while (this.this$0.isInCache(localCapsExtension.getVer()));
          CapsManager.access$1(this.this$0, paramPacket.getFrom(), localCapsExtension.getNode(), localCapsExtension.getVer(), localCapsExtension.getHash());
        }
      }
    }
    , localPacketExtensionFilter);
    this.mConnection.addPacketInterceptor(new PacketInterceptor(this)
    {
      public void interceptPacket()
      {
        DiscoverInfo localDiscoverInfo = CapsManager.access$2(this.this$0);
        if (CapsManager.access$3(this.this$0).size() > 0);
        try
        {
          String str1 = (String)CapsManager.access$3(this.this$0).get(0);
          String str2 = CapsManager.access$4(this.this$0, localDiscoverInfo, str1);
          paramPacket.addExtension(new CapsExtension(str1, CapsManager.access$5(this.this$0), str2));
          return;
        }
        catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
        {
          localNoSuchAlgorithmException.printStackTrace();
        }
      }
    }
    , new PacketTypeFilter(Presence.class));
  }

  private void initSupportedAlgorithm()
  {
    String[] arrayOfString = new String[7];
    arrayOfString[0] = "sha-1";
    arrayOfString[1] = "md2";
    arrayOfString[2] = "md5";
    arrayOfString[3] = "sha-224";
    arrayOfString[4] = "sha-256";
    arrayOfString[5] = "sha-384";
    arrayOfString[6] = "sha-512";
    int i = arrayOfString.length;
    int j = 0;
    if (j >= i)
      return;
    String str = arrayOfString[j];
    try
    {
      MessageDigest.getInstance(str);
      this.mSupportedAlgorithm.add(str);
      ++j;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      while (true)
        System.err.println("Hash algorithm " + str + " not supported");
    }
  }

  private boolean validate(String paramString1, String paramString2, String paramString3, String paramString4)
  {
    int i;
    DiscoverInfo localDiscoverInfo;
    try
    {
      localDiscoverInfo = this.mSdm.discoverInfo(paramString1, paramString2 + "#" + paramString3);
      if (!(this.mSupportedAlgorithm.contains(paramString4)))
      {
        this.mJidCache.put(paramString1, localDiscoverInfo);
        i = 0;
      }
      else
      {
        boolean bool = calculateVer(localDiscoverInfo, paramString4).equals(paramString3);
        if (bool)
        {
          this.mVerCache.put(paramString3, localDiscoverInfo);
          store(paramString3, localDiscoverInfo);
        }
        i = bool;
      }
    }
    catch (XMPPException localXMPPException)
    {
      localXMPPException.printStackTrace();
      i = 0;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      while (true)
        localNoSuchAlgorithmException.printStackTrace();
    }
    return i;
  }

  public DiscoverInfo getDiscoverInfo(String paramString)
  {
    return ((DiscoverInfo)this.mVerCache.get(paramString));
  }

  public DiscoverInfo getDiscoverInfo(String paramString1, String paramString2)
  {
    DiscoverInfo localDiscoverInfo = (DiscoverInfo)this.mVerCache.get(paramString2);
    if (localDiscoverInfo == null)
    {
      localDiscoverInfo = load(paramString2);
      if (localDiscoverInfo == null)
        localDiscoverInfo = (DiscoverInfo)this.mJidCache.get(paramString1);
    }
    return localDiscoverInfo;
  }

  protected boolean isInCache(String paramString)
  {
    return this.mVerCache.containsKey(paramString);
  }

  protected DiscoverInfo load(String paramString)
  {
    return null;
  }

  public void setNode(String paramString)
  {
    this.mNode = paramString;
  }

  protected void store(String paramString, DiscoverInfo paramDiscoverInfo)
  {
  }
}